var $ = getApp(), methods = require("../methods.js");
Page(Object.assign({}, methods, {
	data: {
        leixing:2,
		datalist: [],
    pagenum: 1,
    keywords:'',
	},
	_onLoad:function(){
		var that = this
		if(that.options && that.options.cid){
			that.setData({cid:that.options.cid});
        }
        var leixing = this.data.leixing;
        $.get('yuxun/getmyfatie', { leixing:leixing }, function (res) {
            that.setData(res);
        });
		that.setData({ pagenum: 1, datalist: [], $nomoreData: false });
		that.getDataList();
  },
  onInput(event) {
    this.setData({
      keywords: event.detail.value // 更新 inputValue
    });
  },
  handleAddBook(){
    this.getDataList()
  },

	getDataList: function () {
		var that = this
		var pagenum = that.data.pagenum
		that.setData({loaddingmore:true})
        $.post('yuxun/getmyfatie', { pagenum: pagenum, leixing: that.data.leixing,keywords:that.data.keywords}, function (res) {
			that.setData({loaddingmore:false})
			var data = res.data
			if (pagenum == 1) {
				that.setData({ datalist: data });
				if (data.length == 0) {
					$.showEmpty('暂无相关数据！');
				}
			} else {
				if (data.length == 0) {
					that.setData({ $nomoreData: true });
				} else {
					var datalist = that.data.datalist
					var newdata = datalist.concat(data)
					that.setData({ datalist: newdata });
				}
			}
		},false);
	},
	onReachBottom: function () {
		if (!this.data.$dataEmpty.visible && !this.data.$nomoreData) {
			this.setData({ pagenum: this.data.pagenum + 1 })
			this.getDataList();
		}
    },
    changetab:function(e){
        var that = this;
        var leixing = e.currentTarget.dataset.leixing
        console.log('eee',e.currentTarget.dataset.leixing)
        that.setData({ leixing: leixing })
            that.setData({ pagenum: 1, datalist: [], $nomoreData: false });
            that.getDataList();
    },
    del:function(e){
        var id = e.currentTarget.dataset.id;
        $.post('luntan/deltie', { id: id}, function (res) {
            if(res.status==1){
                var datalist = that.data.datalist;
                datalist.splice(id,1);
                console.log(datalist)
		        that.setData({datalist:datalist});
            }else{
                $.error(res.msg);
            }
        })
        this.getDataList();
    },
    zan:function(e){
        var that = this;
        var id = e.currentTarget.dataset.id;
         // 获取当前项的索引
        const index = this.data.datalist.findIndex(item => item.id === id)
        $.post('luntan/zan', { id: id}, function (res) {
            if(res.status==1){
                // 取反当前项的点赞状态
                if(res.type==1){
                    that.setData({
                        [`datalist[${index}].zan`]: that.data.datalist[index].zan+1,
                        [`datalist[${index}].islike`]: 1
                     })
                }else{
                    that.setData({
                        [`datalist[${index}].zan`]: that.data.datalist[index].zan-1,
                        [`datalist[${index}].islike`]: 0
                     })
                }
                // 判断当前项是否已点赞
                const className = res.type ? 'like' : ''
                const selector = `.list-item:nth-child(${index + 1}) .zan`
                const btn = that.selectComponent(selector)
                btn && btn.setData({ className })
                
            }else{
                $.error(res.msg);return false;
            }
        })
    },
    pinglun:function(e){
        var that = this;
        var id = e.currentTarget.dataset.id;
        $.goto("/d/pages/luntan/detail?id="+id);
    },
    editdiaodian:function(e){
        var that = this;
        console.log('edit111',e.currentTarget.dataset.id)
        const id = e.currentTarget.dataset.id;
        // $.goto("/diaoyu/pages/fatie2?id = " + id);
        $.goto("/diaoyu/pages/fatie2?id=" + id);
    },
    deldiaodian:function(e){
        var that = this;
        var id = e.currentTarget.dataset.id;
        $.post('yuxun/deldiaodian', { id: id}, function (res) {
            if(res.status==1){
                var datalist = that.data.datalist;
                datalist.splice(id,1);
                console.log(datalist)
		        that.setData({datalist:datalist});
            }else{
                $.error(res.msg);
            }
        })
    }
}))